package drds.global_transaction.lock;

import drds.common.Author;
import drds.dbutils.native_sql.Db;
import drds.global_transaction.Authors;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
//业务库都是读未提交   SET GLOBAL TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;
@Author(name = Authors.LI_YANG)
@Slf4j
public abstract class Lock implements ILock
{
    //唯一标识和创建时间
    @Setter
    @Getter
    protected Long id;
    @Setter
    @Getter
    protected long createDateTimeLong;
    //独占锁标志
    @Setter
    @Getter
    protected String tableName;
    @Setter
    @Getter
    protected String primaryKeyColumnValue;
    @Setter
    @Getter
    protected String exclusiveLock;
    //
    @Setter
    @Getter
    protected Integer shareLock;
    //
    @Setter
    @Getter
    protected String globalTransactionId;
    @Setter
    @Getter
    protected String branchTransactionId;

    //
    @Setter
    @Getter
    private boolean isLock;

    //

    @Override
    public abstract void lock() throws SQLException;

    @Override
    public abstract void unlock() throws SQLException;

    protected void insertLock() throws SQLException
    {
        Connection connection = null;
        //
        connection.setAutoCommit(false);
        connection.setAutoCommit(false);
        connection.commit();
        connection.rollback();
        //
        String sql = "select count(0) as total from  global_transaction_lock where table_name = ? and primary_key_column_value =? and exclusive_Lock=? ";//
        List<Object> objectList = new ArrayList<Object>();
        objectList.add(tableName);
        objectList.add(primaryKeyColumnValue);
        objectList.add(exclusiveLock);
        ResultSet resultSet = Db.query(null, sql);
        if (resultSet.next())
        {
        }
        int total = resultSet.getInt("total");
        if (total > 0)
        {
            throw new SQLException();
        }

        String insertSql = "insert into global_transaction_lock (table_name,primary_key_column_value,global_transaction_id,branch_transaction_id,exclusive_lock,share_lock,create_date_time_long)" +
                "values(?,?,?,?,?,?,?)";
        /*insertSql += "'" + tableName + "'," //
                + primaryKeyColumnValue + ",'"//
                + globalTransactionId + "','" //
                + branchTransactionId + "','" //
                + exclusiveLock + "',"//
                + shareLock + ","//
                + createDateTimeLong;//*/
        objectList.clear();
        objectList.add(tableName);
        objectList.add(primaryKeyColumnValue);
        objectList.add(getGlobalTransactionId());
        objectList.add(branchTransactionId);
        objectList.add(exclusiveLock);
        objectList.add(shareLock);
        objectList.add(createDateTimeLong);
        try
        {
            long id = (long) Db.insertWithReturnPrimaryKeyValue(connection, sql, objectList.toArray());
            if (id > 0)
            {
                setId((long) id);
            }
        } catch (Exception e)
        {
            log.error("插入lock失败，totol_sql ={},insert_sql={},total={}", sql, insertSql, total, e);
            throw new SQLException();

        }
    }

    protected void deleteLock() throws SQLException
    {
        Connection connection = null;
        String sql = "delete from global_transaction_lock  where id = ?";
        Db.update(connection, sql, new Object[]{id});

    }

}
